
import { _decorator, Component, Material, dragonBones, Label } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('DragonBonesMode')
export class DragonBonesMode extends Component {

    @property(Material)
    grayMaterial !: Material;
    @property(Material)
    normalMaterial !: Material;
    @property(dragonBones.ArmatureDisplay)
    dragonBones1 !: dragonBones.ArmatureDisplay;
    @property(dragonBones.ArmatureDisplay)
    dragonBones2 !: dragonBones.ArmatureDisplay;
    @property(dragonBones.ArmatureDisplay)
    dragonBones3 !: dragonBones.ArmatureDisplay;
    @property(Label)
    batchLabel !: Label;
    @property(Label)
    cacheLabel !: Label;
    @property(Label)
    grayLabel !: Label;

    _isGray : boolean = false;
    _isBatch : boolean = false;
    _isCache : boolean = false;

    onClickGray (){
        this._isGray = !this._isGray;
        let label = "gray";
        if(this._isGray) label = "normal";
        this.grayLabel.string = label;

        let material = this.grayMaterial;
        if (!this._isGray) {
            material = this.normalMaterial;
        }

        // 设置材质
        this.dragonBones1.setMaterial(material, 0);
        this.dragonBones2.setMaterial(material, 0);
        this.dragonBones3.setMaterial(material, 0);

        // 设置markForUpdateRenderData无效且动画无法正常显示，需要更改动画的缓存模式
        // this.dragonBones1.markForUpdateRenderData(true);
        // this.dragonBones2.markForUpdateRenderData(true);
        // this.dragonBones3.markForUpdateRenderData(true);

        let oldMode = dragonBones.ArmatureDisplay.AnimationCacheMode.SHARED_CACHE;
        let newMode = dragonBones.ArmatureDisplay.AnimationCacheMode.REALTIME;
        if (!this._isCache){
            oldMode = dragonBones.ArmatureDisplay.AnimationCacheMode.REALTIME;
            newMode = dragonBones.ArmatureDisplay.AnimationCacheMode.SHARED_CACHE;
        }
        this.dragonBones1.setAnimationCacheMode(newMode);
        this.dragonBones2.setAnimationCacheMode(newMode);
        this.dragonBones3.setAnimationCacheMode(newMode);

        this.dragonBones1.setAnimationCacheMode(oldMode);
        this.dragonBones2.setAnimationCacheMode(oldMode);
        this.dragonBones3.setAnimationCacheMode(oldMode);
    }

    onClickBatch (){
        this._isBatch = !this._isBatch;
        let label = "batch";
        if (this._isBatch) label = "no batch";
        this.batchLabel.string = label;

        this.dragonBones1._enableBatch = this._isBatch;
        this.dragonBones2._enableBatch = this._isBatch;
        this.dragonBones3._enableBatch = this._isBatch;

        // enableBatch 允许合批未实现导致暂时无法合批
        // this.dragonBones1._updateBatch();
        // this.dragonBones2._updateBatch();
        // this.dragonBones3._updateBatch();
    }

    onClickCache (){
        this._isCache = !this._isCache;

        let label = "cache";
        if (this._isCache) label = "no cache";
        this.cacheLabel.string = label;

        let mode = dragonBones.ArmatureDisplay.AnimationCacheMode.SHARED_CACHE;
        if (!this._isCache) mode = dragonBones.ArmatureDisplay.AnimationCacheMode.REALTIME;
        this.dragonBones1.setAnimationCacheMode(mode);
        this.dragonBones2.setAnimationCacheMode(mode);
        this.dragonBones3.setAnimationCacheMode(mode);
    }
}

